package com.personal.server.impl;

import com.personal.mapper.BlogsMapper;
import com.personal.pojo.Blogs;
import com.personal.pojo.BlogsCover;
import com.personal.server.BlogsServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class BlogsServerImpl implements BlogsServer {

    @Autowired
    private BlogsMapper blogsMapper;

    @Override
    public boolean add(Blogs blogs) {
        log.info("插入数据：{}",blogs);
        blogs.setCreateTime(LocalDate.now());
        blogs.setUpdateTime(LocalDate.now());
        int b = blogsMapper.add(blogs);
        if(b == 0){
            log.info("插入失败");
            return false;
        }
        log.info("插入成功");
        return true;
    }


    @Override
    public List<Blogs> getBlogsByUserId(String id) {
        log.info("查询博客，用户id：{}",id);
        return blogsMapper.getBlogsByUserId(id);
    }

    @Override
    public boolean delete(String id) {
        int b = blogsMapper.delete(id);
        if(b == 0){
            log.info("删除失败");
            return false;
        }
        log.info("删除成功");
        return true;
    }

    @Override
    public boolean update(Blogs blogs) {
        blogs.setUpdateTime(LocalDate.now());
        int b = blogsMapper.update(blogs);
        if(b == 0){
            log.info("更新失败");
            return false;
        }
        log.info("更新成功");
        return true;
    }

    @Override
    public List<Blogs> getAllBlogs() {
        List<Blogs> b = blogsMapper.getAllBlogs();
        if(b == null){
            log.info("查询失败");
            return null;
        }
        log.info("查询成功");
        return b;
    }

    @Override
    public List<Blogs> getBlogsByTitle(int pageNum, int pageSize) {
        List<Blogs> b = blogsMapper.getBlogsByTitle((pageNum - 1) * pageSize, pageSize);
        if(b == null){
            log.info("查询失败");
            return null;
        }
        log.info("查询成功");
        return b;
    }

    @Override
    public List<BlogsCover> getAllBlogsCover() {
        List<BlogsCover> b = blogsMapper.getAllBlogsCover();
        if(b == null){
            log.info("查询失败");
            return null;
        }
        log.info("查询成功");
        return b;
    }

    @Override
    public List<BlogsCover> getMyBlogsCoverById(Integer id) {
        List<BlogsCover> b = blogsMapper.getMyBlogsCoverById(id);
        if(b == null){
            log.info("查询失败");
            return null;
        }
        log.info("查询成功");
        return b;
    }

    @Override
    public Blogs getOneBlogsById(Integer id) {
        Blogs b = blogsMapper.getOneBlogsById(id);
        if(b == null){
            log.info("查询失败");
            return null;
        }
        log.info("查询成功");
        return b;
    }

    @Override
    public List<Blogs> searchBlogsByTitle(String title) {
        List<Blogs> b = blogsMapper.searchBlogsByTitle(title);
        if(b == null){
            log.info("查询失败");
            return null;
        }
        log.info("查询成功");
        return b;
    }

    @Override
    public List<BlogsCover> searchBlogsByBlogsId(List<Blogs> b) {
        List<BlogsCover> bc = new ArrayList<>();
        for (Blogs blogs : b) {
            bc.add(blogsMapper.searchBlogsByBlogsId(blogs.getId()));
        }
        return bc;
    }
}
